// -------------------------------------------------------------
// cookies
// -------------------------------------------------------------

function setKVCookie( name, key, value )
{
	var present = 0;
	var newCookie = "";
	var oldCookie = readCookie( name );
	
	// remove existing key/value pair from cookie
	if( oldCookie != null )
	{
		var pairs = oldCookie.split( "," );
		for( var i = 0; i < pairs.length; i++ )
		{
			var pair = pairs[ i ].split( "=" );
			if( pair.length == 2 && pair[ 0 ] != key )
				newCookie += "," + pairs[ i ];
		}
	}
	
	// add key/value to cookie
	newCookie += "," + key + "=" + value;
		
	// remove first , character
	if( newCookie.charAt( 0 ) )
		newCookie = newCookie.substring( 1, newCookie.length );
	
	writeCookie( name, newCookie, 30 );
}


function readCookie( name )
{
	var nameEq = name + "=";
	var cookies = document.cookie.split( ';' );
	for( var i = 0; i < cookies.length; i++ )
	{
		var c = cookies[ i ];
		while( c.charAt( 0 ) == ' ' )
			c = c.substring( 1, c.length );
		if( c.indexOf( nameEq ) == 0 )
			return c.substring( nameEq.length, c.length );
	}
	return null;
}


function writeCookie( name, value, days )
{
	if( days )
	{
		var date = new Date();
		date.setTime( date.getTime() + (days * 24 * 60 * 60 * 1000) );
		var expires = "; expires=" + date.toGMTString();
	}
	else
		expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}


function deleteCookie( name )
{
	writecookie( name, "", -1 );
}

// -------------------------------------------------------------
// form element event passing
// -------------------------------------------------------------

function focusElementById( id )
{
        var e;

        if (document.all) {
                e = document.all[ id ];
        } else if (document.getElementById) {
                e = document.getElementById( id );
        }

	if( e )
	{
		if( e.focus )
			e.focus();
		if( e.setSelectionRange )
		{
			e.setSelectionRange( 0, e.value.length );
		}
	}
	return false;
}

// -------------------------------------------------------------
// Table hide show rows funcs
// -------------------------------------------------------------
var showMode = 'table-row';
// However, IE5 at least does not render table cells correctly
// using the style 'table-cell', but does when the style 'block'
// is used, so handle this
if (document.all) showMode='block';

function show_table_rows(table_id, row_id, display) {
  var theTable = (document.getElementById(table_id));
  var theTB    = theTable.tBodies.item(0);

  rows = theTB.rows;

  // Apply the style to the CSS display property for the cells

  for(j = 0; j < rows.length; j++) {
    if (rows[j].id == row_id) {
      rows[j].style.display = display?showMode:'none';
    }
  }
}

// -------------------------------------------------------------
// checking element lists
// -------------------------------------------------------------

function checkElementsByName( name, checked )
{
	var elements = document.getElementsByName( name );
	for( var i = 0; i < elements.length; i++ )
	{
		var e = elements[ i ];
		e.checked = checked;
	}
}

function setValuesByName( name, value )
{
	var elements = document.getElementsByName( name );
	for( var i = 0; i < elements.length; i++ )
	{
		var e = elements[ i ];
		e.value = value;
	}
}

// -------------------------------------------------------------
// DIO Editor Funcs
// -------------------------------------------------------------
var isIE=document.all?true:false;
var isDOM=document.getElementById?true:false;
var isNS4=document.layers?true:false;

/* _w : which ID (1) or (2) */
/* _h : (h)ide or (s)how */
function toggleT(w,h,v) {
  if (isDOM) {
    if (h=='h') document.getElementById(w).value=v;
    if (h=='s') document.getElementById(w).value="";
  } else if (isIE) {
    if (h=='h') eval("document.all."+w+".value=v;");
    if (h=='s') eval("document.all."+w+".value='';");
  } else if(isNS4) {
    if (h=='h') eval("document.layers['"+w+"'].value=v;");
    if (h=='s') eval("document.layers['"+w+"'].value='';");
  }
}
function set_group(my_type) {
  var w = my_type.selectedIndex;
  var selected_text = my_type.options[w].text;

  if (selected_text == '') {
    toggleT('group','s',selected_text);
  } else {
    toggleT('group','h',selected_text);
  }
}

function set_type(my_type) {
  var w = my_type.selectedIndex;
  var selected_text = my_type.options[w].text;

  if (selected_text == '') {
    toggleT('cd_namespace','s',selected_text);
  } else {
    toggleT('cd_namespace','h',selected_text);
  }
}
function toggleVis(nspace){

  var theTable = (document.getElementById('DioTable'));
  var theTB    = theTable.tBodies.item(0);

  rows = theTB.rows;

  // Apply the style to the CSS display property for the cells
  for(j = 0; j < rows.length; j++) {
    mode = (nspace == 'All' || rows[j].id == nspace || rows[j].id == '_header')? showMode : 'none';
    rows[j].style.display = mode;
  }
}
function initDioTable() {
  toggleVis(document.forms['d_form'].list_ns[document.forms['d_form'].list_ns.selectedIndex].value);
}

